<?php
namespace App\Http\Controllers\Api\v1\Astro;
use App\Http\Controllers\Api\BaseController;
use App\Http\Controllers\Api\v1\User\AstrosetController;
use App\Http\Controllers\Api\v1\Astro\DataController;

use App\Exceptions\ApiException;
use App\Models\Api\Recode;
use Illuminate\Http\Request;

class ProgressionsvsnatalController extends BaseController
{

    /**
     * @return \think\response\Json
     * @throws \app\common\lib\exception\ApiException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function get(Request $request)
    {
        $Set = new AstrosetController();
        $astro_set = $Set->get(true,'progressionsvsnatal');// 内部调用
        $Data = new DataController();
        $h_sys = $astro_set['value']['h_sys'];
        $Data->planets_display = $astro_set['value']['planets_display'];
        $Data->asps_display = $astro_set['value']['asps_display'];
        $Data->asp_orb_all = $astro_set['value']['asp_orb_all'];
        $Data->planet_orb_all = $astro_set['value']['planet_orb_all'];

        $datetime = htmlspecialchars($request->input('datetime'));

        $time = strtotime($datetime);
        $timeinfo = [
            'year'=>date('Y',$time),
            'month'=>date('m',$time),
            'day'=>date('d',$time),
            'hour'=>date('H',$time),
            'minute'=>date('i',$time),
        ];
        $timeinfo['timezone'] = intval($request->input('timezone'));
        $timeinfo['is_summer'] = isSummerTime($timeinfo['hour'],$timeinfo['minute'],0,
            $timeinfo['month'],$timeinfo['day'],$timeinfo['year']) ? 1 : 0;

        $map = ['id' => $request->input('id', 0), 'uid' => $request->uid];

        $userinfo = (new Recode())->getRecode($map);

        $timestamp1 = $Data->deal_time($userinfo);

        $utdatenow = strftime("%d.%m.%Y", $timestamp1);
        $utnow = strftime("%H:%M:%S", $timestamp1);

        $longitude = ($userinfo['long_deg'] + $userinfo['long_min'] / 60 ) * $userinfo['ew'];
        $latitude = ($userinfo['lat_deg'] + $userinfo['lat_min'] / 60 ) * $userinfo['ns'];

        $exec_str  = config('astrolog.prefix')." -b{$utdatenow} -ut{$utnow} -p0 -eswe -house{$longitude},{$latitude},{$h_sys} -fj -g, -head";
        exec ($exec_str, $out);

        $timestamp2 = $Data->deal_time($timeinfo);

        $birth_JD = gregoriantojd(date('m',$timestamp1), date('d',$timestamp1), date('Y',$timestamp1)) + ((date('H',$timestamp1) + (date('i',$timestamp1) / 60)) / 24);
        $start_JD = gregoriantojd(date('m',$timestamp2), date('d',$timestamp2), date('Y',$timestamp2)) + ((date('H',$timestamp2) + (date('i',$timestamp2) / 60)) / 24);
        $days_alive = $start_JD - $birth_JD; // 总天数
        $prog_time_to_add = $days_alive / 365.2422;// 365.2422  365.2421990  365.25
//        echo $days_alive."<br/>";
        // 夜间盘
        if($out[0] > 0 || $out[0] < 7)
        {
            $time_before_JD=$birth_JD+floor($prog_time_to_add);
        }
        else
        {
            $time_before_JD=$birth_JD+floor($prog_time_to_add);
        }


        //变化比率 //就是$prog_time_to_add的去掉整数的小数
        $time_change_pro=$prog_time_to_add-floor($prog_time_to_add);

        $date_before = strtotime(jdtogregorian($time_before_JD));
        $date_after = strtotime('+1 day',$date_before);

        $utdatenow1 = strftime("%d.%m.%Y", $date_before);
        $utnow1 = strftime("%H:%M:%S", $timestamp1);

        $utdatenow2 = strftime("%d.%m.%Y", $date_after);
        $utnow2 = strftime("%H:%M:%S", $timestamp1);

        $options1 = ['utdatenow' => $utdatenow1, 'utnow' => $utnow1, 'longitude' => $longitude, 'latitude' => $latitude, 'h_sys' => $h_sys];
        $options2 = ['utdatenow' => $utdatenow2, 'utnow' => $utnow2, 'longitude' => $longitude, 'latitude' => $latitude, 'h_sys' => $h_sys];
        $options3 = ['utdatenow' => $utdatenow, 'utnow' => $utnow, 'longitude' => $longitude, 'latitude' => $latitude, 'h_sys' => $h_sys];

        $out1 = $Data->execData($options1);
        $out2 = $Data->execData($options2);
        $out_natal = $Data->execData($options3);


//        $exec_str1 = config('astrolog.prefix')." -b{$utdatenow1} -ut{$utnow1} -p0123456789DAmHt -eswe -house{$longitude},{$latitude},{$h_sys} -flds -g, -head";
//        $exec_str2 = config('astrolog.prefix')." -b{$utdatenow2} -ut{$utnow2} -p0123456789DAmHt -eswe -house{$longitude},{$latitude},{$h_sys} -flds -g, -head";
//        $exec_str_natal  = config('astrolog.prefix')." -b{$utdatenow} -ut{$utnow} -p0123456789DAmHt -eswe -house{$longitude},{$latitude},{$h_sys} -flds -g, -head";
//
//        exec ($exec_str1, $out1);
//        exec ($exec_str2, $out2);
//        exec ($exec_str_natal, $out_natal);
        $calOut = $Data->getProgressionThirdprogressed($out1,$out2,$time_change_pro);

        $original_data1 = $Data->getOriginalData($out_natal,$h_sys);
        $original_data2 = $Data->getOriginalData($calOut,$h_sys);
        $Data->array_remove($original_data1,$Data->planets_display);
        $Data->array_remove($original_data2,$Data->planets_display);
        $details1 = $Data->getPlanetHouseDetail($original_data1, $original_data1['hc1']);
        $details2 = $Data->getPlanetHouseDetail($original_data2, $original_data1['hc1']);
        $phase1 = $Data->planetPkPlanet($original_data1, $original_data2);
        $phase2 = $Data->planetPkPlanet($original_data2, $original_data1);
        foreach ($phase1['planets'] as $k1 => $d1)
        {
            $details1['planets'][$k1]['phase'] = $d1['phase'];
            if(empty($d1['phase'])) $details1['planets'][$k1]['phase'] = null;
        }
        foreach ($phase2['planets'] as $k2 => $d2)
        {
            $details2['planets'][$k2]['phase'] = $d2['phase'];
            if(empty($d2['phase'])) $details2['planets'][$k2]['phase'] = null;
        }
        $details1['phase'] = $phase1['phase'];
        $details2['phase'] = $phase2['phase'];


        $details1['planets'] = (object)$details1['planets'];// 指定输出为object  防止所有行星全开时 类型改变
        $details2['planets'] = (object)$details2['planets'];// 指定输出为object  防止所有行星全开时 类型改变

//        $details['time1'] = date('Y-m-d H:i:s',$date_before);

        return response()->json(['code'=>200,'data'=>[$details1,$details2],'msg'=>'Success.']);
    }
}
